package opn.srn.command.load.parser.util;

import java.util.Map;

import opn.srn.command.load.parser.IParser;
import opn.srn.command.load.parser.ParserFactory;
import opn.srn.command.load.parser.RecordMetaData;

public class ParserUtil {

	public static IParser createParser(RecordMetaData metaData) throws Exception {
		Map<String, String> parameters = metaData.getParameters();
		String parseType = parameters.get("parsertype");

		if (parseType == null) {
			throw new Exception("parsertype attribute is missing in field " + metaData.getName());
		}

		if (ParserFactory.get().isRegistered(parseType) == false) {
			throw new Exception("Parser by name " + parseType + " is not registered...");
		}

		IParser parser = ParserFactory.get().newInstance(parseType);
		parser.load(metaData);
		return parser;
	}

}
